package yourauthor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import yourauthor.utils.ExclusionWordFilter;
import yourauthor.workcomparator.CompareByMostCommonWords;
import yourauthor.workcomparator.CompareByPunctuationRatio;
import yourauthor.workcomparator.WorkComparator;

/**
 *
 * @author ross
 */
public class YourAuthor {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        if ( args.length <= 2 ) {
            printSyntax();
            return;
        }

        String literaryWork1 = args[args.length-1];
        String literaryWork2 = args[args.length-2];
        int numArgs = 0;
        boolean useWordCountMethod = true;
        String exclusionsFile = null;
        for ( int i = 0; i < args.length; i++ ) {
            if ( args[i].equals("-p") ) { //Check word count or punctuation method
                useWordCountMethod = false;
                numArgs++;
            } else if ( args[i].equals("-w") ) {
                numArgs++;
            } else if ( args[i].equals("-e") ) { //Check for an exclusion file
                if ( args.length < i+1 ) {
                    printSyntax();
                    return;
                }
                numArgs+=2;
                exclusionsFile = args[i+1];
            }
        }
        numArgs+=2;
        if ( args.length < numArgs ) {
            printSyntax();
            return;
        }

        if ( exclusionsFile != null )
            try {
                ExclusionWordFilter.addExclusionsFromFile(new FileInputStream(exclusionsFile));
            } catch (FileNotFoundException ex) {
                System.out.println("WARNING: Exclusion file not found!");
            }

        WorkComparator workComparator = null;

        try {
            if ( useWordCountMethod ) 
                workComparator = new CompareByMostCommonWords(new FileInputStream(literaryWork1), new FileInputStream(literaryWork2));
            else
                workComparator = new CompareByPunctuationRatio(new FileInputStream(literaryWork1), new FileInputStream(literaryWork2));

            if ( workComparator.compareWorks() ) {
                System.out.println("The two works are considered comparable");
            } else {
                System.out.println("The two works are not comparable");
            }
        } catch (FileNotFoundException ex) {
            System.out.println("ERROR: File not found");
        }
        
    }
    
    private static void printSyntax() {
        System.out.println("Unrecognized command, usage: YourAuthor [-w|-c] [-e <exclusionFile>] <inputFile1> <inputFile2>");
    }
}